; -------------------------------------------------------------------------

.386
.model flat, stdcall
option casemap:none
; -------------------------------------------------------------------------
include windows.inc
include user32.inc
include kernel32.inc
include advapi32.inc

includelib user32.lib
includelib kernel32.lib
includelib advapi32.lib
; -------------------------------------------------------------------------

include RegUtil.inc
; -------------------------------------------------------------------------

; -------------------------------------------------------------------------
.data
kMsgCaption		db		"TTom's Registry Procedures", 0
kMsgBoxText1	db		"Creating a string key", 0
kMsgBoxText2	db		"Reading the key value", 0
kMsgBoxText3	db		"Creating a double word key", 0
kMsgBoxText4	db		"Reading the double word", 0
kMsgBoxText5	db		"Thats all forks!!!", 0
kErrorText		db		"Registry Error!!!", 0

szKeyName		db		"Software\MASM\Registry Test\", 0
szStringValue	db		"Some String", 0
szRegistryText	db		"This is our text, Cool, huh?", 0
szDWordValue	db		"Some dword", 0
szBigBuffer		db		256 dup(' '), 0
dwMagic			dword	00214948H 	; this is a magic number
									; think of it as 4 ASCII bytes
									; and member in memory it's stored
									; least sig byte first (so the '00'
									; goes last. (Hint: that makes it zero
									; terminated)
dwWordBack		dword	?
dwBigBuffer		dword	SIZEOF szBigBuffer

;DW_SIZE 		EQU		4
; -------------------------------------------------------------------------

; -------------------------------------------------------------------------
.code

start:
	invoke MessageBox, NULL, addr kMsgBoxText1, addr kMsgCaption, MB_OK
	
	; lets open and write a string key
	invoke SetRegString, HKEY_CURRENT_USER, addr szKeyName, addr szStringValue, addr szRegistryText, sizeof szRegistryText
	; check if the registry string value succeeded
	.IF eax!=ERROR_SUCCESS
		invoke MessageBox, NULL, addr kErrorText, addr kMsgBoxText1, MB_OK
		invoke ExitProcess, NULL ; quit on any error
	.ENDIF
	
	invoke MessageBox, NULL, addr kMsgBoxText2, addr kMsgCaption, MB_OK
	
	; now lets read our string value back
	invoke GetRegString, HKEY_CURRENT_USER, addr szKeyName, addr szStringValue, addr szBigBuffer, addr dwBigBuffer
	; check if the registry read succeeded
	.IF eax!=ERROR_SUCCESS
		invoke MessageBox, NULL, addr kErrorText, addr kMsgBoxText2, MB_OK
		invoke ExitProcess, NULL ; quit on any error
	.ENDIF
	; lets display the string we got back, it's now in the big buffer
	invoke MessageBox, NULL, addr szBigBuffer, addr kMsgBoxText2, MB_OK
	
	invoke MessageBox, NULL, addr kMsgBoxText3, addr kMsgCaption, MB_OK
	
	; now we write a double word value
	invoke SetRegDWord, HKEY_CURRENT_USER, addr szKeyName, addr szDWordValue, addr dwMagic
	; check if the registry write succeeded
    .IF eax!=ERROR_SUCCESS
        invoke MessageBox, NULL, addr kErrorText, addr kMsgBoxText3, MB_OK
        invoke ExitProcess, NULL ; quit on any error
    .ENDIF
    invoke MessageBox, NULL, addr kMsgBoxText4, addr kMsgCaption, MB_OK
	
	; now lets read our dword value back
    invoke GetRegDWord, HKEY_CURRENT_USER, addr szKeyName, addr szDWordValue, addr dwWordBack
	; check if the registry read succeeded
    .IF eax!=ERROR_SUCCESS
        invoke MessageBox, NULL, addr kErrorText, addr kMsgBoxText4, MB_OK
        invoke ExitProcess, NULL ; quit on any error
    .ENDIF
	; lets display the dword we got back, it's now in the dwWordBack
    invoke MessageBox, NULL, addr dwWordBack, addr kMsgCaption, MB_OK
	; let's be doubly sure we got the same value back
	mov eax, dwMagic
    .IF dwWordBack != eax
        invoke MessageBox, NULL, addr kErrorText, addr kMsgCaption, MB_OK
        invoke ExitProcess, NULL ; quit on any error
    .ENDIF
	
    invoke MessageBox, NULL, ADDR kMsgBoxText5, ADDR kMsgCaption, MB_OK
	invoke ExitProcess, NULL
end start
; -------------------------------------------------------------------------
